package com.qm.console.pubdef.dto.bx;

import com.qm.console.component.exception.BusinessException;
import com.qm.console.controller.dto.ResultEnum;
import com.qm.console.dao.mapper.BxAtomFieldDOMapper;
import com.qm.console.dao.model.BxAtomFieldDO;
import com.qm.console.dao.model.BxAtomFieldDOExample;
import com.qm.console.dao.model.BxDisplayFieldDO;

import org.springframework.beans.BeanUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import lombok.Getter;
import lombok.Setter;

/**
 * //                       _oo0oo_
 * //                      o8888888o
 * //                      88" . "88
 * //                      (| -_- |)
 * //                      0\  =  /0
 * //                    ___/`---'\___
 * //                  .' \\|     |// '.
 * //                 / \\|||  :  |||// \
 * //                / _||||| -:- |||||- \
 * //               |   | \\\  -  /// |   |
 * //               | \_|  ''\---/''  |_/ |
 * //               \  .-\__  '-'  ___/-. /
 * //             ___'. .'  /--.--\  `. .'___
 * //          ."" '<  `.___\_<|>_/___.' >' "".
 * //         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 * //         \  \ `_.   \_ __\ /__ _/   .-` /  /
 * //     =====`-.____`.___ \_____/___.-`___.-'=====
 * //                       `=---='
 * //
 * //
 * //     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * //
 * //               佛祖保佑         永无BUG
 * <p>
 * com.qm.console.pubdef.dto.bx
 * Created on 2021/1/20.
 */
@Getter
@Setter
public class BxDisplayFieldDTO {
    private Integer id;

    private String fieldName;

    private String desc;

    private String query;

    private List<BxAtomFieldDTO> relyOn;

    private Date createTime;

    private Date updateTime;


    public static BxDisplayFieldDTO fromDO(BxDisplayFieldDO src, BxAtomFieldDOMapper atomFieldDOMapper) {
        BxDisplayFieldDTO result = new BxDisplayFieldDTO();
        BeanUtils.copyProperties(src, result);
        result.setRelyOn(parseAtomFields(src.getRelyOn(), atomFieldDOMapper));
        return result;
    }

    public BxDisplayFieldDO toDo() {
        BxDisplayFieldDO result = new BxDisplayFieldDO();
        BeanUtils.copyProperties(this, result);
        if (relyOn != null) result.setRelyOn(relyOn.stream().map(BxAtomFieldDTO::getId).map(String::valueOf).collect(Collectors.joining(",")));
        return result;
    }

    public static List<BxAtomFieldDTO> parseAtomFields(String atomFields, BxAtomFieldDOMapper bxAtomFieldDOMapper) {
        List<Integer> ids = Arrays.stream(atomFields.split(",")).map(Integer::parseInt).collect(Collectors.toList());
        final BxAtomFieldDOExample example = new BxAtomFieldDOExample();
        example.createCriteria().andIdIn(ids);
        List<BxAtomFieldDO> result = bxAtomFieldDOMapper.selectByExampleWithBLOBs(example);
        return result.stream().map(BxAtomFieldDTO::fromDO).collect(Collectors.toList());
    }


    public void validate(BxAtomFieldDOMapper atomFieldDOMapper) {
        if (relyOn == null || relyOn.isEmpty()) {
            throw new BusinessException(ResultEnum.BX_JOB_FIELD_INVALIDATE);
        } else {
            List<Integer> ids = relyOn.stream().map(BxAtomFieldDTO::getId).collect(Collectors.toList());
            final BxAtomFieldDOExample example = new BxAtomFieldDOExample();
            example.createCriteria().andIdIn(ids);
            int count = atomFieldDOMapper.countByExample(example);
            if (count != ids.size()) {
                throw new BusinessException(ResultEnum.BX_JOB_FIELD_INVALIDATE);
            }
        }
    }
}
